/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package appGUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import mainApp.Data;
import mainApp.Word;
import utils.DataAdapter;
import utils.Utils;

/**
 *
 * @author Picto
 */
public class MenuBarListener implements ActionListener {

    Data data;
    DataAdapter dataAdapter;
    boolean gameInProgress = false;
    Utils utility = new Utils();
    boolean save = true;
    MainPanel mp;
    MainFrame mf;

    public MenuBarListener() {
        utility.checkForHighScoreFile();
        data = new Data();
        dataAdapter = new DataAdapter();
        mp = new MainPanel();
    }

    public void setFrame(MainFrame mf) {
        this.mf = mf;
    }

    public void setHighScore() {
        int highscorePos;
        highscorePos = dataAdapter.addHighScore(String.valueOf(JOptionPane.showInputDialog(null, "Enter username")), mp.data.getScore());
        if (highscorePos != 0) {
            Object[] options = {"Continue", "Highscores", "Quit"};
            int n = JOptionPane.showOptionDialog(null, "Congratulations you got a high score!", "Highscore", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
            if (n == 0) {
            } else if (n == 1) {
                showHighscores();
            } else {
                System.exit(0);
            }
        }
    }

    public void newGame() {
        if (gameInProgress == true) {
            mp.quit();
            mf.remove(mp);
            data.clearAllData();
        } else {
            changeWordList();
        }
        data.setIsGUI(true);
        char[] dst = new char[20];
        data.setScore(0);
        data.getWord().getValue().getChars(0, data.getWord().getWordLength(), dst, 0);
        data.setHighScores(dataAdapter.getHighScores());
        data.setHighScore(data.getHighScores().get(0));
        data.setAttempts(0);
        data.setGuessedCorrectly(false);
        mp = new MainPanel();
        mp.setBounds(0, 20, 1000, 700);
        mf.add(mp);
        mp.initialise(data);
        gameInProgress = true;
    }

    private void changeWordList() {
        int status = 0;
        Object[] possibilities = dataAdapter.getFileNameFromDir(dataAdapter.checkSettings().getProperty("wordListsLocation"));
        while (status == 0) {
            try {
                String s = String.valueOf(JOptionPane.showInputDialog(null, "Select a word list", "Select Word List", JOptionPane.QUESTION_MESSAGE, null, possibilities, "Default"));
                if (!s.equals("null")) {
                    data.setWord(new Word(s));
                    if (gameInProgress == true) {
                        newGame();
                    }
                } else if (gameInProgress == false) {
                    Object[] options = {"Ok", "Add Word File", "Open Editor", "Quit"};
                    int n = JOptionPane.showOptionDialog(null, "Please select a word file to begin game, alternatively you can add a new word file or edit an existing one.", "Error", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
                    if (n == 0) {
                        continue;
                    } else if (n == 1) {
                    } else if (n == 2) {
                    } else {
                        save = false;
                        quitGame();
                    }
                } else if (s.equals("null")) {
                }
            } catch (NullPointerException e) {
                if (gameInProgress == false) {
                    Object[] options = {"Ok", "Add Word File", "Open Editor", "Quit"};
                    int n = JOptionPane.showOptionDialog(null, "Please select a word file to begin game, alternatively you can add a new word file or edit an existing one.", "Error", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
                    if (n == 0) {
                        continue;
                    } else if (n == 1) {
                    } else if (n == 2) {
                    } else {
                        save = false;
                        quitGame();
                    }
                }
            }
            status = 1;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("newGame")) {
            newGame();
        } else if (e.getActionCommand().equals("changeWordList")) {
            changeWordList();
        } else if (e.getActionCommand().equals("quit")) {
            quitGame();
        } else if (e.getActionCommand().equals("about")) {
            showAbout();
        } else if (e.getActionCommand().equals("highscores")) {
            showHighscores();
        } else if (e.getActionCommand().equals("saveGame")) {
            
        } else if (e.getActionCommand().equals("addWordList")) {
            dataAdapter.addNewWordFile();
        } else if (e.getActionCommand().equals("changeWordListFolder")) {
            dataAdapter.setWordListsLocation();
        } else {
            int n = JOptionPane.showConfirmDialog(null, "Submitting a score will reset the current score and start a new game. Do you wish to continue?");
            if (n == 0) {
                setHighScore();
                data.setScore(0);
            } else if (n == 1) {
                JOptionPane.showMessageDialog(null, "Score not submitted.");
            } else {
            }
        }
    }

    public void showAbout() {
        AboutFrame af = new AboutFrame();
        af.setVisible(true);
    }

    public void showHighscores() {
        HighscoresFrame hf = new HighscoresFrame();
        hf.setVisible(true);
    }

    public void quitGame() {
        if (save == true) {
            int n = JOptionPane.showConfirmDialog(null, "Do you wish to quit without submitting your score?");
            if (n == 0) {
                System.exit(0);
            } else if (n == 1) {
                setHighScore();
            } else {
            }
        } else {
            System.exit(0);
        }
    }
}
